import ArrayUtil from '@/ArrayUtil';
import EffectCommon from 'code/Core/Common/EffectCommon';
import SLKCommon from 'code/Core/Common/SLKCommon';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';

const desc = '你的攻击可以额外在|cff1fff443|r个敌人间弹射，伤害衰减|cfffd0e3615%|r|n觉醒后由伤害衰减变为伤害递增';
const art = 'ReplaceableTextures\\CommandButtons\\BTNUpgradeMoonGlaive.blp';
let name = '弹射';

const config: TalentConfigModel = {
    id: 'talent_missile_bonce',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_missile_bonce',
        art,
        ubertip: desc,
        name,
    },
    buffTypePlus: {
        id: 'buff_t_missile_bonce_plus',
        art,
        ubertip: `你的攻击可以额外在|cff1fff443|r个敌人间弹射，伤害递增|cff1fff4415%|r`,
        name: '弹射[觉醒]',
    },
};

export default class TalentMissileBonce extends TalentBase {
    static instance: TalentMissileBonce;
    static missileHit(p: 弹射参数<boolean>) {
        let dmg = UnitCommon.取攻击(p.caster);
        let r = p.other ? 1.15 : 0.85;
        for (let i = 0; i < p.curLoop; i++) {
            dmg *= r;
        }
        DamageSystemInstance.applyAttachAttackDamage(p.target, p.caster, dmg);
    }
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentMissileBonce.instance = this;
        this.setTalentConfig(config);
        EventManager.onTakeAttack(this.attackEffect);
    }
    attackEffect(d: DamageEventData) {
        if (d.isAttach) return;
        let { unEffect, normal, plus } = TalentMissileBonce.instance.talentUnEffect(d.source);
        if (unEffect) return;
        let es = SelectCommon.getAllyUnitInRangeExcludeCenterUnit(d.target, 450);
        if (es.length <= 0) return;
        let target = ArrayUtil.randomElement(es);
        let missile = SLKCommon.getUnitMissileart(d.source);
        let speed = SLKCommon.getUnitMissileSpeed(d.source);
        let radian = SLKCommon.getUnitMissilearc(d.source) ?? 0.15;
        EffectCommon.模拟弹射({
            curX: GetUnitX(d.target),
            curY: GetUnitY(d.target),
            missile,
            speed,
            radian,
            target,
            caster: d.source,
            bounceCount: 3,
            other: plus,
            onHit: TalentMissileBonce.missileHit,
        });
    }
}
